//
//  CommentFunc.swift
//  Wmsmobile
//
//  Created by xuNick on 16/10/11.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

/**
 用来过滤字符串是否是数字，和是否符合规定的浮点精度
 
 - parameter precision: 保留小数点后边几位
 - parameter string:    要过滤的字符串
 
 - returns: 符合要求返回 TRUE
 */
func numFilter(_ precision: Int? , string: String) -> Bool {
    if precision == nil {
        return true
    }
    if precision == 0 {
        for char in string.cString(using: String.Encoding.ascii) ?? [10] {
            if (char < 48 || char > 57) && char != 0 {
                return false
            }
        }
    }else{
        for char in string.cString(using: String.Encoding.ascii) ?? [10] {
            if (char < 45 || char > 57 || char == 47) && char != 0 {
                return false
            }
        }
    }
    if string.contains("."){
        if let little = string.components(separatedBy: ".").last{
            if little.characters.count > precision!{
                return false
            }
        }
    }
    return true
}

func == (lhs: [Dictionary<String, String>]?, rhs: [Dictionary<String, String>]?) -> Bool {
    switch (lhs, rhs) {
    case let (l?, r?):
        guard l.count == r.count else {
            return false
        }
        for index in 0..<l.count {
            if l[index] != r[index]{
                return false
            }
        }
        return true
    case (nil, _?):
        return true
    case (nil, nil):
        return true
    default:
        return false
    }
}

func += <KeyType, ValueType> (left: inout Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
    for (k, v) in right {
        left.updateValue(v, forKey: k)
    }
}

func stringClassFromString(_ className: String) -> AnyClass! {
    
    /// 获取命名空间
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
    
    /// 根据命名空间传来的字符串先转换成anyClass
    let cls: AnyClass = NSClassFromString("\(namespace).\(className)")!;
    
    // 在这里已经可以return了   返回类型:AnyClass!
    return cls;
}


func stringClassFromStringOptional(_ className: String) -> AnyClass? {
    
    /// 获取命名空间
    let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
    
    /// 根据命名空间传来的字符串先转换成anyClass
    let cls: AnyClass? = NSClassFromString("\(namespace).\(className)");
    
    // 在这里已经可以return了   返回类型:AnyClass?
    return cls;
}
